#include "board_widget.h"
#include "ui_board_widget.h"

BoardWidget::BoardWidget(GameEngine *reversi_game, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BoardWidget)
{
    ui->setupUi(this);
    m_block_size = 88;
    m_width = this->width();
    m_height = this->height();
    this->setStyleSheet("background-color:green");
    m_curr_game = reversi_game;


    m_timer.setInterval(1000);
    connect(&m_timer,SIGNAL(timeout()),this,SLOT(cpu_play()));
    m_timer.start();
    minimax = new Minimax(reversi_game);
    minimax->start();
}

BoardWidget::~BoardWidget()
{
    delete ui;
}

void BoardWidget::cpu_play(){
    minimax->cpu_play();
    this->update();
    if(m_curr_game->is_finished()){
        m_timer.stop();
    }
}

void BoardWidget::paintEvent(QPaintEvent *){
    m_painter.begin(this);
    m_painter.setBackground(QBrush(Qt::black));

    draw_grid();
    draw_white_board();
    draw_black_board();
    draw_available_moves();
    draw_scores();

    m_painter.end();
}

void BoardWidget::draw_grid(){
    for(int i = 0; i < 8; i++){
        for(int j = 0; j < 8; j++){
            m_painter.drawLine(i*m_block_size,0,i*m_block_size,m_height);
            m_painter.drawLine(0,j*m_block_size,m_width-200,j*m_block_size);
        }
    }
    m_painter.drawLine(m_width-200,0,m_width-200,m_height);
}

void BoardWidget::draw_white_board(){
    QBrush brush(Qt::white);
    m_painter.setBrush(brush);
    for(int i = 0; i < 8; i++){
        for(int j = 0; j < 8; j++){
            if(m_curr_game->get_board(WHITE_PLAYER)->get_piece(i,j)){
                m_painter.drawEllipse(j*m_block_size+20,i*m_block_size+20,50,50);
            }
        }
    }
}

void BoardWidget::draw_black_board(){
    QBrush brush(Qt::black);
    m_painter.setBrush(brush);
    for(int i = 0; i < 8; i++){
        for(int j = 0; j < 8; j++){
            if(m_curr_game->get_board(BLACK_PLAYER)->get_piece(i,j)){
                m_painter.drawEllipse(j*m_block_size+20,i*m_block_size+20,50,50);
            }
        }
    }
}

void BoardWidget::draw_available_moves(){
    int curr_player = m_curr_game->get_current_player();
    QBrush brush;
    brush.setStyle(Qt::SolidPattern);
    if(curr_player == 0){
        brush.setColor(Qt::white);
    }else{
        brush.setColor(Qt::black);
    }
    m_painter.setBrush(brush);

    std::vector<int> moves;
    m_curr_game->get_available_moves(moves);
    for(unsigned int i = 0; i < moves.size(); i+=2){
        m_painter.drawEllipse(moves[i]*m_block_size+36,moves[i+1]*m_block_size+36,16,16);
    }
}

void BoardWidget::draw_scores(){
    unsigned int white_score = m_curr_game->get_score(0);
    unsigned int black_score = m_curr_game->get_score(1);
    m_painter.setBrush(QBrush(Qt::gray));
    m_painter.drawRect(m_width-200,0,m_width,m_height);
    QFont white_font;
    white_font.setPixelSize(100);
    m_painter.setFont(white_font);
    m_painter.setPen(QPen(Qt::white));
    m_painter.drawText(720,20,200,100,0,QString::number(white_score));
    m_painter.setPen(QPen(Qt::black));
    m_painter.drawText(720,120,200,100,0,QString::number(black_score));
    if(m_curr_game->is_finished()){
        QFont finished_font;
        finished_font.setPixelSize(25);
        m_painter.setFont(finished_font);
        m_painter.drawText(720,320,250,250,0,"FINISHED!");
        if(m_curr_game->get_winner() == 0){
            m_painter.setPen(QPen(Qt::white));
            m_painter.drawText(720,350,250,100,0,"P1 WINS!");
        }
        if(m_curr_game->get_winner() == 1){
            m_painter.setPen(QPen(Qt::black));
            m_painter.drawText(720,350,250,100,0,"P2 WINS!");
        }
        if(m_curr_game->get_winner() == -1){
            m_painter.setPen(QPen(Qt::red));
            m_painter.drawText(720,350,250,100,0,"DRAW!");
        }
    }
}

void BoardWidget::resize(int w, int h){
    m_width = w;
    m_height = h;    
}


void BoardWidget::mousePressEvent(QMouseEvent *event){
    if(!m_curr_game->is_finished()){
        int x = event->x();
        int y = event->y();
        int piece_x_pos = x / m_block_size;
        int piece_y_pos = y / m_block_size;
        if(event->button() == Qt::LeftButton){
            m_curr_game->verify_move(piece_x_pos,piece_y_pos);
        }
    }
    this->update();
}
